   package com.javalong.affairs.module.main.activity;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.gson.reflect.TypeToken;
import com.javalong.affairs.R;
import com.javalong.affairs.base.activity.UIHandlerActivity;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.model.User;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.StringUtils;
import com.javalong.affairs.base.utils.ToastUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushManager;

@ContentView(R.layout.activity_register)
public class RegisterActivity extends UIHandlerActivity {
	@ViewInject(R.id.bt_register)
	private Button bt_register;
	@ViewInject(R.id.et_phone)
	private EditText et_phone;
	@ViewInject(R.id.et_pwd)
	private EditText et_pwd;
	@ViewInject(R.id.et_username)
	private EditText et_username;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		bt_register.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		switch (v.getId()) {
		case R.id.bt_register:
			if (checkEdit()) {
				String url = UrlUtils.getUrl("User", "insert");
				Map<String, Object> params = new HashMap<String, Object>();
				params.put(RequestKey.USERNAME, et_username.getText()
						.toString());
				params.put(RequestKey.PASSWORD, et_pwd.getText().toString());
				params.put(RequestKey.PHONE, et_phone.getText().toString());
				progressDialog.show();
				HttpUtils.post(url, params, new BaseResponseListener(
						new TypeToken<User>() {
						}.getType()) {
					@Override
					public void onSuccess(Object obj) {
						super.onSuccess(obj);
						User user = (User) obj;
						IntentManager.doActivity(RegisterActivity.this,
								LoginActivity.class);
						XGPushManager.registerPush(RegisterActivity.this,
								user.userName, new XGIOperateCallback() {
									@Override
									public void onSuccess(Object arg0, int arg1) {
									}

									@Override
									public void onFail(Object arg0, int arg1,
											String arg2) {
										Log.v("test", arg2);
									}
								});
						// ToastUtils.showMessage("注册成功！");
						progressDialog.dismiss();
					}
				});
			}
			break;
		default:
			break;
		}
	}

	private boolean checkEdit() {
		if (StringUtils.isEmpty(et_username.getText().toString())
				|| StringUtils.isEmpty(et_pwd.getText().toString())
				|| StringUtils.isEmpty(et_phone.getText().toString())) {
			ToastUtils.showMessage("请输入注册信息");
			return false;
		}
		return true;
	}
}
